import { _decorator, Collider2D,find,RigidBody2D, Component, Contact2DType, Node, Vec3,IPhysics2DContact, Prefab, instantiate } from 'cc';
import { BlockType, DIR } from '../Common';
import { TankCamp } from './TankCamp';
import { Block } from './Block';
import { AudioMgr } from '../../AudioMgr/AudioMgr';
import { Healthy } from './Healthy';
import { EventMgr } from '../../EventMgr/EventMgr';
import { Vector3 } from '../Math/Vector3';
const { ccclass, property } = _decorator;

@ccclass('ShootBullet')
export class ShootBullet extends Component {

    @property isEnemy:boolean=false;

    @property(Prefab)
     dieEffect:Prefab=null;

    @property speed=5.6; 
    @property dir:Vec3 =DIR.left;

    @property damage:number=1;


    private collider:Collider2D=null;

    private tCamp:TankCamp=null;

    protected onLoad(): void {
        this.tCamp=this.getComponent(TankCamp);
        this.collider=this.getComponent(Collider2D);
        
        this.collider.on(Contact2DType.BEGIN_CONTACT,this.onBeginContact,this);


        this.scheduleOnce(()=>{
            this.node.active=false;
        this.node.getComponent(RigidBody2D).enabled=false;
            this.node.destroy();
        },20);
    }

    protected onDestroy(): void {
        this.collider.off(Contact2DType.BEGIN_CONTACT,this.onBeginContact,this);
        
    }

    onBeginContact(self:Collider2D,other:Collider2D,  contact: IPhysics2DContact | null){
        let tc =other.getComponent(TankCamp);
        if(tc&&this.tCamp.canHit(tc)){

            //console.log("selfname"+self.node.name);
            let rb2d=self.node.getComponent(RigidBody2D);
            this.scheduleOnce(()=>{
               
                rb2d.enabled=false;
            },0.02);

            let bl:Block|null =other.getComponent(Block);
            let hl:Healthy|null =other.getComponent(Healthy);
            let sb:ShootBullet|null =other.getComponent(ShootBullet);
            if(bl){
                let blType:BlockType=bl.getBlockType();
                switch(blType){
                    case BlockType.wall:
                        if(!this.isEnemy)AudioMgr.Inst.playEffectSync("shoot");
                        //销毁自己
                        this.destroySelf();
                        //块销毁
                        bl.destorySelf();
                        break;
                    case BlockType.stone:
                        if(!this.isEnemy)AudioMgr.Inst.playEffectSync("bin_tank");
                        this.destroySelf();
                        if(this.damage>1){
                            bl.destorySelf();
                        }
                        break;
                    case BlockType.camp:
                        AudioMgr.Inst.playEffectSync("blast");
                        EventMgr.Inst.sendEvent("GAME_OVER");
                        this.destroySelf();
                        bl.destorySelf();
                        break;
                    case BlockType.river:
                        break;
                    case BlockType.ice:
                        break;
                    case BlockType.forest:
                        break;
                }
            }else if(hl){
                if(!this.isEnemy) AudioMgr.Inst.playEffectSync("bin_tank");
                hl.reduceHp(this.damage);
                this.destroySelf(false);
               // console.log("可能是敌人")
            }else if(sb){
                this.destroySelf();
            }
           // console.log("开始碰撞了");
        }
    }

    destroySelf(needEffect:boolean=true){
        //console.log("effect die"+this.dieEffect);
        /*
        if(needEffect){
      let n:Node=   instantiate(this.dieEffect)
        find("Canvas").addChild(n);
        let {x,y}=this.node.getPosition();
        n.setPosition(x,y);
        }else{

        }
        */
        this.scheduleOnce(()=>{
            this.node.active=false;
        this.node.getComponent(RigidBody2D).enabled=false;
            this.node.destroy();
        },0.02);
       // this.node.destroy();
        //AudioMgr.Inst.playEffectSync("hit");
    }

    init(dir:Vec3,pos:Vec3,damage:number=1){
        this.damage=damage;
        this.changeDir(dir);
        let {x,y}=pos;
        this.node.setPosition(x,y);
       if(!this.isEnemy) AudioMgr.Inst.playEffectSync("fire");
    }

    changeDir(dir:Vec3){
        this.dir =dir;
        this.updateDir();
            }
        
    updateDir(){
        if(this.dir===DIR.left){
            this.node.setRotationFromEuler(new Vec3(0,0,0));
        }else if(this.dir===DIR.top){
            this.node.setRotationFromEuler(new Vec3(0,0,-90));
        }else if(this.dir===DIR.right){
            this.node.setRotationFromEuler(new Vec3(0,0,-180));
        }else if(this.dir ===DIR.bottom){
            this.node.setRotationFromEuler(new Vec3(0,0,-270));
        }
    }

    changePos(deltaTime:number){
        let {x,y}=this.node.position;
        if( Vector3.ValEqual(this.dir,DIR.left) ){
            x-=deltaTime*this.speed;
        }else if(Vector3.ValEqual(this.dir,DIR.top) ){
            y+=deltaTime*this.speed;
            this.node.setRotationFromEuler(new Vec3(0,0,-90));
        }else if(Vector3.ValEqual(this.dir,DIR.right) ){
            x+=deltaTime*this.speed;
            this.node.setRotationFromEuler(new Vec3(0,0,-180));
        }else if(Vector3.ValEqual(this.dir,DIR.bottom) ){
            y-=deltaTime*this.speed;
            this.node.setRotationFromEuler(new Vec3(0,0,-270));
        }
        this.node.setPosition(x,y);

    }

    update(deltaTime: number) {
        this.changePos(deltaTime);   
    }
}


